package cn.wit.tech.service.impl;


import cn.wit.tech.condition.SearchTechDirectionCondition;
import cn.wit.tech.mapper.ProjectExerciseMapper;
import cn.wit.tech.mapper.TechDirectionMapper;
import cn.wit.utils.pojo.ProjectExercise;
import cn.wit.utils.pojo.TechDirection;
import cn.wit.tech.service.TechDirectionService;
import cn.wit.utils.response.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class TechDirectionServiceImpl implements TechDirectionService {
    @Autowired
    private TechDirectionMapper techDirectionMapper;
    @Autowired
    private ProjectExerciseMapper projectExerciseMapper;


    @Override
    public Result insert(TechDirection techdirection) {
        int i = techDirectionMapper.insert(techdirection);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("新增失败");
    }

    @Override
    public Result deleteById(int id) {
        QueryWrapper<ProjectExercise> wrapper = new QueryWrapper<>();
        wrapper.eq("techdirection_ID",id);
        List<ProjectExercise> projectExercise = projectExerciseMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(projectExercise)){
            int i = techDirectionMapper.deleteById(id);
            if(i > 0){
                return Result.ok();
            }
        }
        return Result.error("删除失败");
    }

    @Override
    public Result updateById(TechDirection techdirection) {
        int i = techDirectionMapper.updateById(techdirection);
        if(i > 0){
            return Result.ok();
        }
        return Result.error("更新失败");
    }


    @Override
    public Result selectAllTechDirection() {
        List<TechDirection> techDirections = techDirectionMapper.selectList(null);
        if(!CollectionUtils.isEmpty(techDirections)){
            return Result.ok(techDirections);
        }
        return Result.ok(new ArrayList<>());
    }

    @Override
    public Result searchPage(IPage<TechDirection> page, SearchTechDirectionCondition condition) {
        QueryWrapper<TechDirection> wrapper3 = new QueryWrapper<>();
        if(condition != null) {
            if(condition.getDirectionName() != null && condition.getDirectionName() != "") {
                wrapper3.like("direction_Name", condition.getDirectionName());
            }
            if(condition.getJob() != null && condition.getJob() != "") {
                wrapper3.like("job", condition.getJob());
            }
        }
        IPage<TechDirection> techDirectionIPage = techDirectionMapper.selectPage(page, wrapper3);
        return Result.ok(techDirectionIPage);
    }
}
